package com.yb.module.model.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yb.framework.core.base.BaseMapper;
import com.yb.framework.core.base.BaseServiceImpl;
import com.yb.framework.core.utils.SecurityUtil;
import com.yb.framework.core.utils.SnowflakeIdWorker;
import com.yb.framework.core.utils.StringUtil;
import com.yb.framework.neo4j.service.Neo4jService;
import com.yb.module.model.domain.ModelTemplate;
import com.yb.module.model.domain.ModelTemplateShape;
import com.yb.module.model.mapper.ModelTemplateMapper;
import com.yb.module.model.mapper.ModelTemplateParamMapper;
import com.yb.module.model.mapper.ModelTemplateShapeMapper;
import com.yb.module.model.service.ModelTemplateService;

@Service
public class ModelTemplateServiceImpl extends BaseServiceImpl<ModelTemplate> implements ModelTemplateService {

	@Autowired
	private Neo4jService neo4jService;
	@Autowired
	private ModelTemplateMapper modelTemplateMapper;
	@Autowired
	private ModelTemplateParamMapper modelTemplateParamMapper;
	@Autowired
	private ModelTemplateShapeMapper modelTemplateShapeMapper;
	
	@Override
	protected BaseMapper<ModelTemplate> getMapper() {
		return modelTemplateMapper;
	}

	@Override
	public Long getParentId(Long id) {
		// TODO Auto-generated method stub
		return modelTemplateMapper.getParentId(id);
	}

	@Override
	public List<Long> getChildIds(Long id) {
		// TODO Auto-generated method stub
		return modelTemplateMapper.getChildIds(id);
	}

	@Override
	public List<Long> getAllParentIds(Long id, List<Long> parentIds) {
		// TODO Auto-generated method stub
		Long pId = modelTemplateMapper.getParentId(id);
		if(pId != null) {
			parentIds.add(pId);
			parentIds = getAllParentIds(pId, parentIds);
		}
		return parentIds;
	}

	@Override
	public List<Long> getAllChildIds(Long id, List<Long> childIds) {
		// TODO Auto-generated method stub
		List<Long> cIds = modelTemplateMapper.getChildIds(id);
		if(cIds != null && cIds.size() > 0) {
			childIds.addAll(cIds);
			for (Long cId : cIds) {
				childIds = getAllChildIds(cId, childIds);
			}
		}
		return childIds;
	}

	@Override
	public int insertTemplate(ModelTemplate modelTemplate) {
		// TODO Auto-generated method stub
		long id = SnowflakeIdWorker.getInstance().getId();
		neo4jService.createTemplateNode("template", String.valueOf(id), modelTemplate.getName(), 
				String.valueOf(modelTemplate.getParentId()), StringUtil.longArray2StrArray(modelTemplate.getShapeIds()));
		
		modelTemplate.setId(id);
		modelTemplate.setCreateBy(SecurityUtil.getUsername());
		modelTemplate.setCreateTime(new Date());
		int insetResult = modelTemplateMapper.insert(modelTemplate);
		
		Long[] shapeIds = modelTemplate.getShapeIds();
		if(shapeIds != null && shapeIds.length > 0) {
			for (Long shapeId : shapeIds) {
				ModelTemplateShape modelTemplateShape = new ModelTemplateShape();
				modelTemplateShape.setTemplateId(id);
				modelTemplateShape.setShapeId(shapeId);
				modelTemplateShapeMapper.insert(modelTemplateShape);
			}
		}
		
		return insetResult;
	}

	@Override
	public int updateTemplate(ModelTemplate modelTemplate) {
		// TODO Auto-generated method stub
		neo4jService.updateNode("template", String.valueOf(modelTemplate.getId()), modelTemplate.getName(), 
				String.valueOf(modelTemplate.getParentId()), StringUtil.longArray2StrArray(modelTemplate.getShapeIds()));
		
		modelTemplate.setUpdateBy(SecurityUtil.getUsername());
		modelTemplate.setUpdateTime(new Date());
		int updateResult = modelTemplateMapper.update(modelTemplate);
		
		modelTemplateShapeMapper.deleteByTemplateId(modelTemplate.getId());
		Long[] shapeIds = modelTemplate.getShapeIds();
		if(shapeIds != null && shapeIds.length > 0) {
			for (Long shapeId : shapeIds) {
				ModelTemplateShape modelTemplateShape = new ModelTemplateShape();
				modelTemplateShape.setTemplateId(modelTemplate.getId());
				modelTemplateShape.setShapeId(shapeId);
				modelTemplateShapeMapper.insert(modelTemplateShape);
			}
		}
		
		return updateResult;
	}

	@Override
	public int deleteTemplate(Long[] ids) {
		// TODO Auto-generated method stub
		for (Long id : ids) {
			neo4jService.deleteNode("template", String.valueOf(id));
		}
		
		for (Long id : ids) {
			modelTemplateParamMapper.deleteByTemplateId(id);
			modelTemplateShapeMapper.deleteByTemplateId(id);
		}
		return modelTemplateMapper.deleteListByIds(ids);
	}
}